﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/
using Caliburn.Micro;
using OI.Async;
using OI.Framework;

namespace OIManager.Async
{
    public class JurisdictionRemove: IResult
    {
        #region DATA
        public int JurisdictionID { get; private set; }
        public string ConnectionString { get; private set; }
        public string Results { get; set; }
        #endregion

        public event System.EventHandler<ResultCompletionEventArgs> Completed;


        public JurisdictionRemove(int jurisID, string connectionString)
        {
            JurisdictionID = jurisID;
            ConnectionString = connectionString;
        }


        public void Execute(ActionExecutionContext context)
        {
            var client = ServiceClients.CreateCatalogServiceClient();

            client.RemoveJurisdictionCompleted += (s, e) => ProcessResults(e.Error, e.Result);

            client.RemoveJurisdictionAsync(JurisdictionID, ConnectionString);
        }


        private void ProcessResults(System.Exception ex, string msg)
        {
            if (ex != null)
            {
                IoC.Get<IShell>().HandleException(ex, "Error deleting OIJurisdiction");
            }

            Results = msg;
            Completed(this, new ResultCompletionEventArgs { Error = ex });
        }
    }
}
